import cv2
import numpy as np

from turbojpeg import TurboJPEG, TJPF_GRAY, TJSAMP_GRAY, TJFLAG_PROGRESSIVE, TJFLAG_FASTUPSAMPLE, TJFLAG_FASTDCT


jpeg = TurboJPEG()

# decoding input.jpg to BGR array
in_file = open('181781-56x100.jpg', 'rb')
bgr_array = jpeg.decode(in_file.read(),flags=TJFLAG_FASTUPSAMPLE|TJFLAG_FASTDCT)
in_file.close()
out_file = open('output.jpg', 'wb')
out_file.write(jpeg.encode(bgr_array,quality=100))
out_file.close()
# from PIL import Image
# from PIL.ExifTags import TAGS

# im = Image.open("\\\\172.16.16.16\\车间\\写真图库\\AA-SJ\\26x30\\10287.jpg")

# width, height = im.size
# print(im.size)
# try:
#     dpi = im.info['dpi']
#     print("dpi",dpi)
# except:

#     #如果直接获取失败，再尝试获取exif信息
#     t = {}
#     info = im._getexif()

#========
# # 读取图像
# img = cv2.imread('C:\\Users\\wyq\\Desktop\\181781-56x100.jpg')

# # 设置 JPEG 编码参数
# encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 100]

# # 获取图片尺寸
# h, w, c = img.shape

# # 设置 DPI（每英寸点数）
# dpi = 300

# # 创建 8.5x11英寸的画布
# canvas = np.zeros((int(w), int( h), 3), np.uint8)

# # 将图片复制到画布中央
# canvas = img

# # 保存 JPEG 格式图片，指定 DPI
# cv2.imencode('.jpg', canvas, encode_param)[1].tofile('C:\\Users\\wyq\\Desktop\\output.jpg', )